var pageMiniWidth = 1024;//页面最小宽度
$(document).ready(function() {

	$("#tabs").KandyTabs({
		action : "fade",
		trigger : "click",
		custom : function(btn, cont, index, tab) {
			// btn;console.log(index);
			index ? $("#userLogin")[0].reset() : $("#muserLogin")[0].reset();
			index ? $('#usersafecode').css('display', 'none') : $('#adminsafecode').css('display', 'none');
		},
		delay : 100
	});

	// top bar
	var top = $("#topbar");
	top.children(".topcontent").css({
		lineHeight : top.css("height")
	});
	var topBackground = $("<div></div>", {
		id : "topBackground"
	});
	topBackground.css({
		height : top.height()
	}).appendTo(top);


	//body宽度不跟随document
	$(window).resize(function(){
		if($(document).width()<=1024 - getScrollWidth()){
			$("#topBackground").width($(document).width());
		}else{
			$("#topBackground").css({width:""});
		}
	});
	$(window).resize();
	//---body宽度不跟随document
	
	// 鼠标滑过按钮或者图像
	$(".altImage").each(function(index, element) {
				var ti = $(this);
				ti.data().src = ti.attr("src");
				ti.data().srcalt = ti.attr("srcalt");
				if (!ti.data().srcalt) {
					return;
				}
				ti.data().srcAlt = ti.attr("src");
				ti.bind("mouseover", function() {
							var tii = $(this);
							tii.attr("src", ti.data().srcalt);
						});
				ti.bind("mouseout", function() {
							var tii = $(this);
							tii.attr("src", tii.data().src);
						});
			});

	// 表单
	var formTableOp = $("#registerFormDiv,#manchertuser-window,#user-window");
	formTableOp.find("table").attr("width", "95%");
	formTableOp.find("tr").each(function(itr) {
		var $tr = $(this);
		$tr.find("td").each(function(indextd, td) {
					var $td = $(td);
					if (!$td.hasClass("noauto")) {
						$td.addClass(indextd % 2 == 0
								? "formLabelTd"
								: "formItemTd");
					}
				});
	});

	// 弹出框
	// $("#registButton,#forgetPasswardButton,#forgetMerPasswardButton").fancybox({hideOnOverlayClick:false,
	// padding:10, overlayOpacity:0.2});
	// reset持卡用户找回密码表单
	$("#forgetPasswardButton").click(function() {
				$("#com_form")[0].reset();
			});
	$("#forgetMerPasswardButton").click(function() {
				$("#com_merchantform")[0].reset();
			});
	$("#registButton").click(function() {
				$("#regsitForm")[0].reset();
			});

	// 必须填项星号追加
	$(".mustFill").each(function(index, element) {
				var t = $(this);
				t.append($("<span class='xinghao'>*</span>"));
			});

	// 注册表单验证
	var fm = $("#regsitForm").Validform({
		//btnSubmit : "#submit",
		postonce 			: 	false,
		tiptype 			: 	validFormConfig.tiptype_img,
		checkWhenNoChange 	: 	true, // 自增属性，属性没有变化的时候依然验证
		datatype 			: 	idnoVerify,
		//ajaxPost			:	true,
		beforeSubmit 		: 	function() {
			fm.resetStatus();
			$(window).trigger("regformSubmit");
		}
	});
			
	$("#submit").click(function(){
		fm.submitForm();
	});

	// 登录表单验证
	$("#userLogin,#muserLogin").Validform({
		tiptype : function(msg, obj, cssctl, isTriggerByBlur) { // 第四个参数，是否由blur触发
			if (obj.type != 2 && !isTriggerByBlur) {
				alert(msg);
			}
		}
	});

	// 收藏夹
	$("#shoucang").click(function() {
			addFavorite(document.location.href, document.title);
	});
	$("#shouye").click(function() {
	
	});

	$.ajax({
		type : "POST",
		url : basePath + '/loginservlet.do?method=IdTypeList',
		cache : false,
		data : "",
		success : function(data) {
			data = $.parseJSON(data);
			if (null == data) {
				alert("webservice连接异常，检查webservice是否启动或网络连通!");
				return;
			}
			$(window).trigger("onIdtypeGot",[data]);
			for (var i = 0; i < data.length; i++) {
				$("#IdType").append('<option value="'
						+ data[i].idTypeId + '">' + data[i].idTypeName
						+ "</option>");
			}
			
		}
	});

	$(window).bind("regformSubmit", function() {
		$('#submit').hide();
		var regLogonName = $V('regLogonName');
		var regLogonPwd = $V('regLogonPwd');
		var verifyCode = $V('regsafecode');
		jQuery.ajax({
			url : basePath
					+ 'cardHolderUserPreServlet.do?method=RegisterCardHolderUser',
			data : $('#regsitForm').serialize(),
			type : "POST",
			dataType : 'text',
			success : function(data, status) {
				if (data == "true") {
					if (confirm("持卡用户 [ " + regLogonName + " ] 注册成功,直接以此用户登录吗？")) {
						window.location.href = basePath
								+ "loginservlet.do?method=CardHolderUserLogon&logonName="
								+ regLogonName + "&logonPasswd=" + regLogonPwd
								+ "&verifyCode=" + verifyCode;
					} else {
						document.forms.regsitForm.reset();
						$(window).trigger("EV_fancyClose");
					}
				} else {
					$('#submit').show();
					alert(data);
				}
			},
			error : function(data, status) {
				alert(data);
			}
		});
	});
});

// 验证码
$("#usersafecode,#adminsafecode").each(function(index, element) {
	var ti = $(this), aa = ti.next();
	aa.data("safecodeimg", ti);
	aa.attr("href", "javascript:void(0);");
	aa.click(function() {
				var tii = $(this);
				tii.data().safecodeimg.attr("src", basePath
								+ "servlet/ValImgServlet?d="
								+ (new Date()).getTime());
			});
	ti.attr("src", basePath + "servlet/ValImgServlet");
});

// 持卡用户找回密码时加载密码问题
function doUserDatil() {
	var username = $V('chuLogonName');
	
	//清空表单
	var pform = $("#chuloginname").parents("form");
	if(pform[0] && pform[0].reset) pform[0].reset();
	
	$E('chuloginname').value = username;
	if (null == username || "" == username) {
		alert("用户名不能为空!");
		setTimeout(function() {
					$.fancybox.hideActivity();
				}, 80);
		return;
	}
	$.ajax({
		type : "POST",
		url : basePath
				+ '/loginservlet.do?method=CardUserDatilByUserName',
		cache : false,
		data : 'username=' + username,
		success : function(data) {
			data = $.parseJSON(data);
	
			var i = 0;
			$('#question1').html(' ');
			$('#question2').html(' ');
			$('#question3').html(' ');
			if (!$isNull(data)) {
				var question1 = data.question1;
				if (!$isNull(question1)) {
					$('#question1').html(question1);
					// $('#answer1').attr('disabled','');
					i = 1;
				} else {
					// $('#answer1').attr('disabled','disabled');
				}
				var question2 = data.question2;
				if (!$isNull(question2)) {
					$('#question2').html(question2);
					// $('#answer2').attr('disabled','');
					i = 1;
				} else {
					// $('#answer2').attr('disabled','disabled');
				}
				var question3 = data.question3;
				if (!$isNull(question3)) {
					$('#question3').html(question3);
					// $('#answer3').attr('disabled','');
					i = 1;
				} else {
					// $('#answer3').attr('disabled','disabled');
				}
				if (i == 0) {
					$(window).trigger("EV_fancyHideLoading", "user-window");
					alert('该用户您没有密码保护!所以不能通过该方式找回密码!请联系管理员!重置密码!');
					return;
				}
				$(window).trigger("EV_fancyOpen", "user-window");
			} else {
				$(window).trigger("EV_fancyHideLoading", "user-window");
				//alert(data);
				// $('#answer1').attr('disabled','disabled');
				// $('#answer2').attr('disabled','disabled');
				// $('#answer3').attr('disabled','disabled');
				alert('该用户不存在!');
				return;
			}
		}
	});
}
function doUpdate() {
	var username = $V('chuloginname');
	if ($isNull(username)) {
		alert('用户名不能为空!');
		return;
	}
	if (username.length < 6) {
		// alert('用户名长度不能小于6位!');
		// return;
	}
	var newpassword = $V('newpassword');
	if ($isNull(newpassword)) {
		alert('新密码不能为空!');
		return;
	}
	if (newpassword.length < 6) {
		alert('新密码长度不能小于6位!');
		return;
	}
	var qrpassword = $V('qrpassword');
	if ($isNull(qrpassword)) {
		alert('确认密码不能为空!');
		return;
	}
	if (qrpassword.length < 6) {
		alert('确认长度不能小于6位!');
		return;
	}
	if (qrpassword != newpassword) {
		alert('确认密码与新密码不符!');
		return;
	}
	var question1 = $('#question1').text();
	question1 = question1.replace(/\s+/g, "")
	if (!$isNull(question1)) {
		var answer1 = $V('answer1');
		if ($isNull(answer1)) {
			alert('请输入答案1!');
			return;
		}
	}
	var question2 = $('#question2').text();
	question2 = question2.replace(/\s+/g, "")
	if (!$isNull(question2)) {
		var answer2 = $V('answer2');
		if ($isNull(answer2)) {
			alert('请输入答案2!');
			return;
		}
	}
	var question3 = $('#question3').text();
	question3 = question3.replace(/\s+/g, "")
	if (!$isNull(question3)) {
		var answer3 = $V('answer3');
		if ($isNull(answer3)) {
			alert('请输入答案3!');
			return;
		}
	}

	$.ajax({
				type : "POST",
				url : basePath + "/loginservlet.do?method=FindPasword",
				cache : false,
				data : {
					username : username,
					newpassword : newpassword,
					answer1 : answer1,
					answer2 : answer2,
					answer3 : answer3
				},
				success : function(data) {
					if (data == "0") {
						alert("密码找回成功!");
						// $('#user-window').css("display","none");
						// $("#forgetPasswardButton").fancybox.close();
						$.fancybox.close("#forgetPasswardButton");
					} else {
						alert(data);
						return;
					}
				}
			});
}
// 商户找回密码
function doMerUserDatil() {
	var merchantusername = $V('mrchOperLogonName');
	//清空表单
	var pform = $("#merchantloginname").parents("form");
	if(pform[0] && pform[0].reset) pform[0].reset();
	$E('merchantloginname').value = merchantusername;
	if ($isNull(merchantusername)) {
		alert('用户名不能为空!');
		setTimeout(function() {
					$.fancybox.hideActivity();
				}, 80);
		return;
	}
	$.ajax({
				type : "POST",
				url : basePath
						+ '/loginservlet.do?method=MerchantDatilByUserName',
				cache : false,
				data : 'merchantusername=' + merchantusername,
				success : function(data) {
					data = $.parseJSON(data);
					var i = 0;
					$('#merchantquestion1').html(' ');
					$('#merchantquestion2').html(' ');
					$('#merchantquestion3').html(' ');
					$(window).trigger("EV_fancyHideLoading");
					if (!$isNull(data)) {
						$(window)
								.trigger("EV_fancyOpen", "manchertuser-window");
						var merchantquestion1 = data.question1;
						if (!$isNull(merchantquestion1)) {
							$('#merchantquestion1').html(merchantquestion1);
							// $('#answer1').attr('disabled','');
							i = 1;
						} else {
							// $('#answer1').attr('disabled','disabled');
						}
						var merchantquestion2 = data.question2;
						if (!$isNull(merchantquestion2)) {
							$('#merchantquestion2').html(merchantquestion2);
							// $('#answer2').attr('disabled','');
							i = 1;
						} else {

							// $('#answer2').attr('disabled','disabled');
						}
						var merchantquestion3 = data.question3;
						if (!$isNull(merchantquestion3)) {
							$('#merchantquestion3').html(merchantquestion3);
							// $('#answer3').attr('disabled','');
							i = 1;
						} else {
							// $('#answer3').attr('disabled','disabled');
						}
						if (i == 0) {
							alert('该用户您没有密码保护!所以不能通过该方式找回密码!请联系管理员!重置密码!');
							return;
						}
					} else {
						// $('#answer1').attr('disabled','disabled');
						// $('#answer2').attr('disabled','disabled');
						// $('#answer3').attr('disabled','disabled');
						alert('该用户不存在!');
						return;
					}
				}
			});
}
function doMerUpdate() {
	var merchantusername = $V('merchantloginname');
	if ($isNull(merchantusername)) {
		alert('用户名不能为空!');
		return;
	}
	if (merchantusername.length < 6) {
		// alert('用户名长度不能小于6位!');
		// return;
	}
	var merchantnewpassword = $V('merchantnewpassword');
	if ($isNull(merchantnewpassword)) {
		alert('新密码不能为空!');
		return;
	}
	if (merchantnewpassword.length < 6) {
		alert('新密码长度不能小于6位!');
		return;
	}
	var merchantqrpassword = $V('merchantqrpassword');
	if ($isNull(merchantqrpassword)) {
		alert('确认密码不能为空!');
		return;
	}
	if (merchantqrpassword.length < 6) {
		alert('确认长度不能小于6位!');
		return;
	}
	if (merchantqrpassword != merchantnewpassword) {
		alert('确认密码与新密码不符!');
		return;
	}
	var merchantquestion1 = $('#merchantquestion1').text();
	merchantquestion1 = merchantquestion1.replace(/\s+/g, "")
	if (!$isNull(merchantquestion1)) {
		var merchantanswer1 = $V('merchantanswer1');
		if ($isNull(merchantanswer1)) {
			alert('请输入答案1!');
			return;
		}
	}
	var merchantquestion2 = $('#merchantquestion2').text();
	merchantquestion2 = merchantquestion2.replace(/\s+/g, "")
	if (!$isNull(merchantquestion2)) {
		var merchantanswer2 = $V('merchantanswer2');
		if ($isNull(merchantanswer2)) {
			alert('请输入答案2!');
			return;
		}
	}
	var merchantquestion3 = $('#merchantquestion3').text();
	merchantquestion3 = merchantquestion3.replace(/\s+/g, "")
	if (!$isNull(merchantquestion3)) {
		var merchantanswer3 = $V('merchantanswer3');
		if ($isNull(merchantanswer3)) {
			alert('请输入答案3!');
			return;
		}
	}

	$.ajax({
				type : "POST",
				url : basePath + "/loginservlet.do?method=MerchantFindPasword",
				cache : false,
				data : {
					merchantusername : merchantusername,
					merchantnewpassword : merchantnewpassword,
					merchantanswer1 : merchantanswer1,
					merchantanswer2 : merchantanswer2,
					merchantanswer3 : merchantanswer3
				},
				success : function(data) {
					if (data == "0") {
						alert("密码找回成功!");
						// $('#user-window').css("display","none");
						// $("#forgetPasswardButton").fancybox.close();
						$.fancybox.close("#forgetPasswardButton");

					} else {
						alert(data);
						return;
					}
				}
			});
}

// ////////////////密码强度////////////
function zzjs_net(obj) {
	var t = obj.value;
	var id = getResult(t);
	// 定义对应的消息提示
	var msg = new Array(4);
	msg[0] = "密码过短。";
	msg[1] = "密码强度差。";
	msg[2] = "密码强度良好。";
	msg[3] = "密码强度高。";
	var sty = new Array(4);
	sty[0] = -45;
	sty[1] = -30;
	sty[2] = -15;
	sty[3] = 0;
	var col = new Array(4);
	col[0] = "gray";
	col[1] = "#50AEDD";
	col[2] = "#FF8213";
	col[3] = "green";
	// 设置显示效果
	var bImg = "images/navibg_05.png" // 一张显示用的图片
	var sWidth = 300;
	var sHeight = 15;
	var Bobj = document.getElementById("www_zzjs_net");
	// 
	Bobj.style.fontSize = "12px";
	Bobj.style.color = col[id];
	Bobj.style.width = sWidth + "px";
	Bobj.style.height = sHeight + "px";
	Bobj.style.lineHeight = sHeight + "px";
	Bobj.style.background = "url(" + bImg + ") no-repeat left " + sty[id]
			+ "px";
	Bobj.style.textIndent = "20px";
	Bobj.innerHTML = "检测提示：" + msg[id];
}
// 定义检测函数,返回0/1/2/3分别代表无效/差/一般/强
function getResult(s) {
	if (s.length < 4) {
		return 0;
	}
	var ls = 0;
	if (s.match(/[a-z]/ig)) {
		ls++;
	}
	if (s.match(/[0-9]/ig)) {
		ls++;
	}
	if (s.match(/(.[^a-z0-9])/ig)) {
		ls++;
	}
	if (s.length < 6 && ls > 0) {
		ls--;
	}//
	return ls
}
(function($) {
	// 获取子元素内，name为特定值的元素
	$.fn.getFi = function(formItmeName) {
		return this.find("*[name=" + formItmeName + "]");
	};
})(jQuery);
/* 添加收藏夹 */
function addFavorite(url, title) {
	var fav_url = url;
	var fav_title = title;
	if (document.all && window.external) {// IE浏览器添加方式
		window.external.AddFavorite(fav_url, fav_title);
	} else {
		if (window.sidebar) {// 火狐浏览器添加方式
			window.sidebar.addPanel(fav_title, fav_url, "");
		} else {
			alert("\u6d4f\u89c8\u5668\u4e0d\u652f\u6301\uff0c\u8bf7\u624b\u52a8\u52a0\u5165\u6536\u85cf\u5939");
		}
	}
}